package com.xulinhao.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xulinhao.commonutils.R;
import com.xulinhao.edu.entity.Teacher;
import com.xulinhao.edu.entity.vo.TeacherQuery;
import com.xulinhao.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author xulinhao
 * @since 2021-08-03
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 查询所有讲师的所有信息
     * @return JSON
     */
    @ApiOperation(value = "讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<Teacher> list = teacherService.list(null);
//        try {
//            int i = 1/0;
//        } catch (Exception e) {
//            throw new GuliException(20001,"出现自定义异常");
//        }
        return R.ok().data("items", list);
    }

    /**
     * 根据id删除指定的讲师
     * @param id 讲师ID
     * @return JSON
     */
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 分页查询
     * @param current 当前页
     * @param limit 一页显示几行数据
     * @return JSON
     */
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit){
        Page<Teacher> teacherPage = new Page<>(current, limit);
        teacherService.page(teacherPage, null);
        long total = teacherPage.getTotal();
        List<Teacher> records = teacherPage.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 条件分页查询
     * @param current 当前页
     * @param limit 一页显示多少行
     * @param teacherQuery 条件类
     * @return JSOn
     */
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建Page对象
        Page<Teacher> teacherPage = new Page<>(current, limit);
        // 构建条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        //wrapper.实现多条件组合查询(这些条件值可能有.可能没有)
        // mybatis学过 动态SQL
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        // 判断条件值是否为空,如果不为空,则拼接条件
        if (!StringUtils.isEmpty(name)) {
            // 构建条件
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            // 构建条件
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            // 构建条件
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            // 构建条件
            wrapper.le("gmt_create", end);
        }

        // 排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现条件查询分页
        teacherService.page(teacherPage, wrapper);
        long total = teacherPage.getTotal(); // 总记录数
        List<Teacher> records = teacherPage.getRecords(); // 数据list集合
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 添加讲师
     * @param teacher 老师对象
     * @return JSON
     */
    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher){

        boolean b = teacherService.save(teacher);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 根据ID查询讲师
     * @param id 讲师ID
     * @return JSON
     */
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("item", teacher);
    }

    /**
     * 根据ID修改讲师
     * @param teacher   讲师对象
     * @return  JSON
     */
    @ApiOperation(value = "根据ID修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }





}

